/*
 * Copyright (C) 2009 CityLights Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.schizoidman.android.citylights.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;

public class TCServerProxy extends TCEntityLoader {
	
    private List<TCProjectProxy> mProjects = new ArrayList<TCProjectProxy> ();

	public TCServerProxy(Intent intent) {
		super (intent);
	}

	protected void loadFromJSON(String jsonString) throws JSONException
	{
		// A Simple JSONObject Creation
		JSONObject jsProjects=new JSONObject(jsonString);

        // ---  Workaround: When there's just one element, JSON returns one object instead of array
        JSONObject jsProject = jsProjects.optJSONObject("project");
        if (null != jsProject) {
        	mProjects.add(new TCProjectProxy(getData(), jsProject));
        }

		JSONArray jsProjectsArray=jsProjects.optJSONArray("project");
        if (null != jsProjectsArray) {
            for(int i=0;i<jsProjectsArray.length();i++)
            {
            	mProjects.add(new TCProjectProxy(getData(), jsProjectsArray.getJSONObject(i)));
            }
        }
	}
	
	public List<TCProjectProxy> getProjects() {
		return mProjects;
	}

}